package com.sdry.web.controller.zm;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.sdry.model.HospitalEntity;
import com.sdry.model.MaterielEntity;
import com.sdry.model.zm.ZmGeneralQueryEntity;
import com.sdry.service.zm.ZmAtzmService;

@Controller
@RequestMapping("atzm")
public class ZmauthorizationManagement {
	
	@Resource
	ZmAtzmService zmAtzmService;

	@RequestMapping("toAtzm")
	public ModelAndView toLog() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("zm/authorizationManagement");
		return mav;
	}
	/**
	 * 查询医院列表
	 *  调用医院页面提供方法
	 * @param query
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getAtzmList")
	public String getAtzmList(ZmGeneralQueryEntity query) {
		String rtn = null;
		String hospitalName = query.getHospitalName();
		String supplierName = query.getSupplierName();
		List<HospitalEntity> hptList = null;
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		List<MaterielEntity> atzmList = null;
		JSONArray jsonArray = null;
		if(hospitalName!=null || supplierName!=null){
			// 当满足此条件时，是搜索按钮检索
			hptList = zmAtzmService.getHptList(query);
			jsonArray = JSONArray.fromObject(hptList,jsonConfig);
		}
		

		
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", jsonArray.size());
		rtn = jsonObject.toString();
		return rtn;
	}
	/**
	 * 查询医院对应供应商列表
	 * @param query
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getAtzmList2")
	public String getAtzmList2(ZmGeneralQueryEntity query) {
		String rtn = null;
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		if(query.getHospitalId()==null){
			jsonObject.put("code", 500);
			jsonObject.put("msg", "未传递参数");
			jsonObject.put("count", 0);
			jsonObject.put("data", "");
			rtn = jsonObject.toString();
			return rtn;
		}
		List<MaterielEntity> AtzmList2 = null;
		JSONArray jsonArray = JSONArray.fromObject(AtzmList2,jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", AtzmList2.size());
		rtn = jsonObject.toString();
		return rtn;
	}

	/**
	 * 获得穿梭框所需要的数据
	 *  @param hospitalId 医院id
	 * @return 供应商列表，包含是否已被该医院所授权
	 */
	@ResponseBody
	@RequestMapping("getAtzmForT")
	public String getAtzmForT(String hospitalId) {
		String rtn = null;
		List<MaterielEntity> list = zmAtzmService.getAtzmForT(hospitalId);
		rtn = JSON.toJSONString(list);
		return rtn;
	}

	/**
	 * 编辑提交授权
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("editAtzm")
	public String editAtzm(ZmGeneralQueryEntity query) {
		String rtn;
		int flag = 0;
		int flag1 = 0;
		if(query.getHospitalId() == null){
			return "0";
		}
		// 这里应该做一个事务管理
		if (query.getT_add() != null) {
		 	flag =  zmAtzmService.addAtzm(query.getT_add(), query.getHospitalId());
		}
		if (query.getT_del() != null) {
			flag1 = zmAtzmService.delAtzm(query.getT_del(), query.getHospitalId());
		}
		if(flag!= 0&&flag1!=0){
			rtn = flag+flag1+"";
			return rtn;
		}
		return "0";
	}
	

}
